import statService from '@/api/stat/stat'

let data = new class {
  // 数据大小范围内任务条数分布

  constructor () {
    this.fileSizeStr = '10,50,100,500'
    this.xAxisData = this._getXAxisData()
    // this.test = this._getXAxisData()
    this.data = []

    // this.createData()
  }

  setConfig (param) {
    // 为整个类设置个性化参数
    // this.handleType=param.handleType
    return this
  }

  _getXAxisData () {
    let fileSizeArr = this.fileSizeStr.split(',')
    let newArr = []
    for (let i = 0; i < fileSizeArr.length - 1; i++) {
      newArr[i] = fileSizeArr[i] + '-' + fileSizeArr[i + 1] + 'M'
    }
    newArr.splice(0, 0, '<' + fileSizeArr[0] + 'M')
    newArr.push('>' + fileSizeArr[fileSizeArr.length - 1] + 'M')
    return newArr
  }

  async getData (dataType) {
    let param = { dataType: dataType, fileSizeStr: this.fileSizeStr }
    let { success, data } = await statService.querySizeCount(param)

    return { success, data }

  }

  async countIn () {
    let res = await this.getData(0)
    if (res.success) {
      let count = []
      let me = this
      // me.xAxisData=[]
      res.data['import'].data.forEach(item => {
        // me.xAxisData.push(parseInt(item.curHour));
        count.push(parseInt(item.count))
      })
      return count
    }
  }

  async countOut () {
    let res = await this.getData(1)
    if (res.success) {
      let count = []
      let me = this
      res.data['export'].data.forEach(item => {
        count.push(parseInt(item.count))
      })
      return count
    }
  }

  async createData () {
    let { data: importData } = await this.getData(0)
    let { data: exportData } = await this.getData(1)
    return {
      'xAxisData': this.xAxisData,
      'data': {
        'import': {
          name: '导入',
          data: importData
        },
        'export': {
          name: '导出',
          data: exportData
        }
      }
    }
  }

  async createTableData () {
    let { data: importData } = await this.getData(0)
    let { data: exportData } = await this.getData(1)
    let dataArr = []
    let columns = [{
      title: '数据范围',
      key: 'scope',
      ellipsis: true
    },
      {
        title: '导入',
        key: 'import',
        ellipsis: true

      },
      {
        title: '导出',
        key: 'export',
      },
      {
        title: '总数/条',
        key: 'num'
      }]
    for (let i = 0; i < importData.length; i++) {
      let itemObj = {}
      itemObj.scope = this.xAxisData[i]
      itemObj.import = importData[i]
      itemObj.export = exportData[i]
      itemObj.num = parseInt(importData[i]) + parseInt(exportData[i])
      itemObj.ellipsis = true
      dataArr.push(itemObj)
    }
    return {
      data: dataArr,
      columns
    }

  }
}

export {
  data
}
